package com.example.redissondemo;

import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

@RestController
@RequestMapping("test")
public class TestController {

    @RequestMapping("/task")
    public void task() {
        Boolean res = false;
        try {
            res = RedissLockUtil.tryLock("lock-key", 1, 15000);
            System.out.println("拿到锁干活");
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (res) {//释放锁
                RedissLockUtil.unlock("lock-key");
                System.out.println("释放锁");
            }
        }
    }
}
